package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import util.po.ConfigPO;

public final class Configuration {

	public Properties props;

	private String fileName = "config.properties";

	private Logger log = Logger.getLogger(Configuration.class.getName());

	public Configuration() {
		log.setLevel(Level.INFO);
	}

	public ConfigPO configure() {
		ConfigPO configInfo = null;
		try {
			InputStream input = Configuration.class.getResourceAsStream("/"
					+ fileName);
			props = new Properties();
			props.load(input);
			Iterator<Entry<Object, Object>> iter = props.entrySet().iterator();
			while (iter.hasNext()) {
				Entry<Object, Object> entry = iter.next();
				log.info("key--> " + entry.getKey() + ", value is "
						+ entry.getValue());
			}
			configInfo = new ConfigPO();

			configInfo.setCorePoolSize(Integer.parseInt(props
					.getProperty("thread.corePoolSize")));
			log.info("getterthread.corePoolSize  "
					+ configInfo.getCorePoolSize());
			configInfo.setMaximumPoolSize(Integer.parseInt(props
					.getProperty("thread.maximumPoolSize")));
			log.info("getter thread.maximumPoolSize  "
					+ configInfo.getMaximumPoolSize());
			configInfo.setKeepAliveTime(Integer.parseInt(props
					.getProperty("thread.keepAliveTime")));
			log.info("getter thread.keepAliveTime  "
					+ configInfo.getKeepAliveTime());
			configInfo.setBlockingqueueCapacity(Integer.parseInt(props
					.getProperty("thread.blockingqueueCapacity")));
			log.info("getter thread.blockingqueueCapacity   "
					+ configInfo.getBlockingqueueCapacity());
			TimeUnit timeUnit = Enum.valueOf(TimeUnit.class,
					props.getProperty("thread.TimeUnit"));
			configInfo.setTimeUnit(timeUnit);
			log.info("getter timeUnit  " + configInfo.getTimeUnit());

			configInfo.setReceivePath(props.getProperty("socket.receive"));
			log.info("getter socket.receive  " + configInfo.getReceivePath());

			configInfo.setSendPath(props.getProperty("socket.sendPath"));
			log.info("getter socket.sendPath  " + configInfo.getSendPath());

			configInfo.setServerSocketAddress(props
					.getProperty("socket.serverSocketAddress"));
			log.info("getter socket.serverSocketAddress   "
					+ configInfo.getServerSocketAddress());

			configInfo.setServerSocketPort(Integer.valueOf(props
					.getProperty("socket.serverSocketPort")));
			log.info("getter socket.serverSocketPort   "
					+ configInfo.getServerSocketPort());

			configInfo.setServer2SocketAddress(props
					.getProperty("socket.server2SocketAddress"));
			log.info("getter socket.server2SocketAddress  "
					+ configInfo.getServer2SocketAddress());

			configInfo.setServer2SocketPort(Integer.parseInt(props
					.getProperty("socket.server2SocketPort")));
			log.info("getter socket.server2SocketPort   "
					+ configInfo.getServer2SocketPort());

			configInfo.setReceiveBufferSize(Integer.parseInt(props
					.getProperty("socket.receiveBufferSize")));
			log.info("getter socket.receiveBufferSize  "
					+ configInfo.getReceiveBufferSize());

			configInfo.setSendBufferSize(Integer.parseInt(props
					.getProperty("socket.sendBufferSize")));
			log.info("getter socket.sendBufferSize  "
					+ configInfo.getSendBufferSize());

			configInfo.setReceiveExtName(props
					.getProperty("socket.receiveExtName"));
			log.info("getter socket.receiveExtName   "
					+ configInfo.getReceiveExtName());

			configInfo.setServer2SocketReceive(props
					.getProperty("socket.server2receive"));
			log.info("getter socket.server2receive  "
					+ configInfo.getServer2SocketReceive());
		} catch (IOException ex) {
			log.setLevel(Level.WARNING);
			log.warning(ex.fillInStackTrace().getLocalizedMessage());
			ex.printStackTrace();
		}
		return configInfo;
	}

	public ConfigPO configure(String fileName) {
		this.fileName = fileName;
		return configure();
	}

}
